package com.wbao.dianniu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.RecruitManagerAdapter;
import com.wbao.dianniu.data.RecruitAddData;
import com.wbao.dianniu.data.RecruitListData;
import com.wbao.dianniu.listener.IRecruitListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.update.RecruitListChangeManager;
import com.wbao.dianniu.update.RecruitListChangeManager.RecruitChangedListener;
import com.wbao.dianniu.manager.RecruitListManager;
import com.wbao.dianniu.utils.Notification;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位管理
 *
 * @author yejianjun
 */
public class RecruitManagerActivity extends BaseActivity implements IRecruitListListener, OnClickListener, RecruitChangedListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;

    private PullToRefreshListView pullListView;
    private ListView mListView;
    private List<RecruitListData> mList;
    private static RecruitManagerAdapter adapter;
    private RecruitListManager manager;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private TextView noDataTV;
    private Button rightBtn;
    private RecruitListChangeManager recruitListChangeManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_my_apply_recruit, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.post_manager));
        init();
        initWattingView();
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.add));
        rightBtn.setOnClickListener(this);

        manager = new RecruitListManager(this);
        manager.addRecruitListener(this);
        reqData(currentPage);
        initData();
        recruitListChangeManager = RecruitListChangeManager.getInstance();
        recruitListChangeManager.addRecruitChangeListener(this);
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
    }

    private void init() {
        noDataTV = findViewById(R.id.no_find_data);

        pullListView = findViewById(R.id.apply_recruit_listView);
        pullListView.setMode(Mode.BOTH);
        pullListView.setOnRefreshListener(onRefreshListener);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);
    }

    private void initData() {
        mList = new ArrayList<RecruitListData>();
        adapter = new RecruitManagerAdapter(this);
        mList = new ArrayList<>();
        adapter.setData(mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(itemClickListener);
    }

    /*OnRefreshListener onRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };*/
    PullToRefreshBase.OnRefreshListener2 onRefreshListener = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh() {
            pullDownRequest();
        }

        @Override
        public void onPullUpToRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    private void pullDownRequest() {
        currentPage = 0;
        reqData(currentPage);
    }

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        ++currentPage;
        reqData(currentPage);
    }

    private void reqData(int page) {
        manager.recruitList(GlobalContext.getAccountId(), 2, page * PAGE_COUNTS, PAGE_COUNTS, "", "", "", "");
    }

    OnItemClickListener itemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.putExtra(Const.INTENT_JOB_ID, mList.get(position - 1).getId());
            intent.setClass(RecruitManagerActivity.this, RecruitDetailsActivity.class);
            startActivity(intent);
        }
    };

    @Override
    public void onRecruitListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onRecruitListSuccess(List<RecruitListData> list) {
        pullListView.onRefreshComplete(false, false);
        waittingLayout.setVisibility(View.GONE);
        if (null != list) {
            if (0 != currentPage) {
                adapter.addData(list);
                mList = adapter.getList();
            } else {
                mList = list;
                adapter.setData(mList);
            }
            adapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_button:
                if (!GlobalContext.getIndustryAuthority()) {
                    Notification.toast(this, getResources().getString(R.string.incomplete_industry_auth_1));
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(this, RecruitReleaseActivity.class);
                startActivity(intent);
                break;
            case R.id.back_button:
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public void recruitAddChanged(RecruitListData data) {
        noDataTV.setVisibility(View.GONE);
        mList.add(data);
        adapter.setData(mList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void recruitRemoveChanged(int recruitId) {
        for (int i = 0; i < mList.size(); i++) {
            if (recruitId == mList.get(i).getId()) {
                mList.remove(i);
                adapter.notifyDataSetChanged();
                if (mList.size() == 0) {
                    noDataTV.setVisibility(View.VISIBLE);
                } else {
                    noDataTV.setVisibility(View.GONE);
                }
                break;
            }
        }
    }

    @Override
    public void recruitModifyChanged(int recruitId, RecruitAddData data) {
        // TODO Auto-generated method stub
        for (int i = 0; i < mList.size(); i++) {
            if (recruitId == mList.get(i).getId()) {
                mList.get(i).setPost(data.getPost());
                mList.get(i).setTryout(data.getTryout());
                mList.get(i).setSalary(data.getSalary());
                mList.get(i).setAddress(data.getAddress());
                mList.get(i).setExperience(data.getExperience());
                mList.get(i).setEducation(data.getEducation());
                mList.get(i).setDescribe(data.getDescribe());
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != recruitListChangeManager) {
            recruitListChangeManager.removeRecruitChangeListener(this);
        }
        if (null != manager) {
            manager.removeRecruitListener(this);
        }
    }

    public static void StatusChange(int id, int status) {
        adapter.updateStatus(id, status);
    }

}
